package agentes;

import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.util.LinkedList;

import test.Coordinate;
import test.Polygon;
import test.Test;
import alice.tuprolog.MalformedGoalException;
import alice.tuprolog.Prolog;
import alice.tuprolog.SolveInfo;
import alice.tuprolog.Struct;
import alice.tuprolog.Theory;

public class Agent {
	
	Prolog engine = Test.engine;
	LinkedList<Polygon> polygonList = new LinkedList<Polygon>();

	public Agent(){
		Theory t;
		try {
			t = new Theory(new FileInputStream(new File(
					"prologLib/drawPolygons.pl")));
			engine.addTheory(t);
		} catch (Exception e) {
		}
	}
	
	public void setEngine(Prolog engine){
		this.engine = engine;
	}
	
	public void drawPolygon(Polygon p) throws Exception{
		SolveInfo info = engine.solve("draw_polygon("+p.getName()+","+ p.getV() + "," + p.getColorString() +",no_texture).");
		System.out.println(info.isSuccess() + ":" + info.getSolution());
		
	}
	
	public LinkedList<Polygon> getAllPolygons() throws Exception{
		LinkedList<Polygon> polyList = new LinkedList<Polygon>();
		SolveInfo info = engine.solve("polygon(Name,Vertices,Color,Texture,Layer).");
		while (info.isSuccess()){
			Struct s = (Struct) info.getTerm("Vertices");
			LinkedList<Coordinate> coordList = Test.listVertices(s);
			Color c = Test.stringToColor(Test.colorStrip(info.getTerm("Color").toString()));
			
			polyList.add(new Polygon(info.getTerm("Name").toString(), coordList,c));
			
			
			if (engine.hasOpenAlternatives())
				info = engine.solveNext();
			else
				break;
		}
		
		return polyList;
	}
	
}
